package com.gaga.console.core.controller.sys;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.core.entity.SysDict;
import com.gaga.console.core.service.SysDictService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户信息表 前端控制器
 *
 * @author liujia
 * @since 2025-03-27
 */
@RestController
@RequestMapping(ApiPath.ROOT + "/sys/dict")
public class SysDictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 获取列表
     */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<SysDict> data) {

        LambdaQueryWrapperExt<SysDict> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysDict::getDelFlag, DelFlag.OK.getCode());
        data.buildSpecWrap(wraps);

        IPage<SysDict> page = sysDictService.page(data.buildPage(), wraps);

        return Result.ok(data.buildTableResult(page));
    }

    /**
     * 根据详细信息
     */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result getEntity(@PathVariable Long id) {
        return Result.ok(sysDictService.getById(id));
    }


    @ApiOperation(value = "通过字典分类Code获取数据", httpMethod = "GET")
    @GetMapping(path = "/type/{categoryCode}")
    public Result getDictInfoByCategoryCode(@PathVariable("categoryCode") String categoryCode) {

        LambdaQueryWrapperExt<SysDict> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysDict::getDelFlag, DelFlag.OK.getCode())
                .eq(SysDict::getCategoryCode, categoryCode)
                .orderByDesc(SysDict::getSeqNo);
        List<SysDict> list = sysDictService.list(wraps);
        if (CollectionUtils.isEmpty(list)) {
            return Result.ok();
        }
        return Result.ok(list);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysDict entity) {
        return toAjaxBoolean(sysDictService.saveOrUpdate(entity));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return toAjaxBoolean(false);
        }
        List<SysDict> removeEntityList = Arrays.stream(ids).map(id -> {
            SysDict entity = new SysDict().setId(id);
            entity.setDelFlag(DelFlag.DELETED.getCode());
            entity.setDeleteTime(new Date());
            return entity;
        }).collect(Collectors.toList());
        return toAjaxBoolean(sysDictService.updateBatchById(removeEntityList));
    }

}
